package cn.wetcland.urban.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.wetcland.urban.admin.dto.AdminLoginDTO;
import cn.wetcland.urban.admin.mapper.UtAdminMapper;
import cn.wetcland.urban.admin.mapper.UtCustomerMapper;
import cn.wetcland.urban.admin.mapper.UtDriverMapper;
import cn.wetcland.urban.admin.service.UtAdminService;
import cn.wetcland.urban.admin.vo.AdminLoginVO;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.utils.bcrypt.BCryptPasswordEncoder;
import cn.wetcland.urban.generator.domain.UtAdmin;
import cn.wetcland.urban.generator.domain.UtCustomer;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author Wetcland
* @description 针对表【ut_admin】的数据库操作Service实现
* @createDate 2024-01-21 09:51:59
*/
@Service
public class UtAdminServiceImpl extends ServiceImpl<UtAdminMapper, UtAdmin> implements UtAdminService {

    @Resource
    private UtAdminMapper adminMapper;

    @Resource
    private UtCustomerMapper customerMapper;

    @Resource
    private UtDriverMapper driverMapper;

    @Override
    public AdminLoginVO handleAdminLogin(AdminLoginDTO adminLoginDTO) {
        UtAdmin adminUsername = adminMapper.selectOne(new QueryWrapper<UtAdmin>().eq("admin_username", adminLoginDTO.getUsername()));
        MyAssert.notNull(adminUsername, "该管理员不存在");
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(adminLoginDTO.getPassword(), adminUsername.getAdminPassword());
        MyAssert.isTrue(matches, "密码错误");
        StpUtil.login(adminUsername.getAdminId());
        AdminLoginVO adminLoginVO = new AdminLoginVO();
        adminLoginVO.setAdminUsername(adminUsername.getAdminUsername());
        adminLoginVO.setToken(StpUtil.getTokenValue());
        adminLoginVO.setAdminId(adminUsername.getAdminId());
        return adminLoginVO;
    }


}




